Skip to content

Operator Assignment Support#214

Merged
rcosta358 merged 3 commits into
mainfrom
operator-assignments
May 11, 2026
Merged

Operator Assignment Support#214
rcosta358 merged 3 commits into
mainfrom
operator-assignments

Conversation

@rcosta358
Copy link
Copy Markdown
Collaborator

Description

This PR adds support for Java operator assignments such as +=, -=, *=, /=, and %= in the refinement checking.
It works by modeling operator assignments as equivalent simple assignments: x += yx = x + y.

Example

@Refinement("_ > 0")
int plus(@Refinement("_ >= 0") int x) {
    x += 1; // x is now > 0
    return x;
}

Before we had an error in the return statement because the x += 1 was ignored. Now we don't.

Related Issue

None.

Type of change

  • Bug fix
  • New feature
  • Documentation update
  • Code refactoring

Checklist

  • Added/updated tests under liquidjava-example/src/main/java/testSuite/ (Correct* / Error*)
  • mvn test passes locally
  • Updated docs/README if behavior or API changed

@rcosta358 rcosta358 requested a review from CatarinaGamboa May 9, 2026 17:36
@rcosta358 rcosta358 self-assigned this May 9, 2026
@rcosta358 rcosta358 added the enhancement New feature or request label May 9, 2026
Copy link
Copy Markdown
Collaborator

@CatarinaGamboa CatarinaGamboa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to cover more operations on the RHS, and more examples that fail the refinements.
Also, there are more similar ops that we are not targetting right? like bitwise &=, ..., we don't need but it should be a todo or maybe we should throw an error or something say we dont support them

if (literal.getValue() == null)
throw new CustomError("Null literals are not supported", literal.getPosition());
return new Predicate(literal.getValue().toString(), element);
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if it is another kind of expression? we need more fallbacks.
Counter-example (with your example class):

    int t3(@Refinement("_ >= 0") int x) {
        @Refinement("_ == 10 || _ == 11")
        int y = 10;
        y += remainder(x); // x is now >= 6
        return x;
    }

outputs

Error: class com.microsoft.z3.BoolExpr cannot be cast to class com.microsoft.z3.ArithExpr (com.microsoft.z3.BoolExpr and com.microsoft.z3.ArithExpr are in unnamed module of loader org.codehaus.mojo.exec.URLClassLoaderBuilder$ExecJavaClassLoader @cb7fa71)

}

@Override
public <T, A extends T> void visitCtOperatorAssignment(CtOperatorAssignment<T, A> assignment) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throws LJError?

x %= 2; // x is now == 0 || x is now == 1
return x;
}
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also get some negative cases for examples?

@rcosta358
Copy link
Copy Markdown
Collaborator Author

You're right. Will fix it.

@rcosta358 rcosta358 requested a review from CatarinaGamboa May 11, 2026 11:35
Copy link
Copy Markdown
Collaborator

@CatarinaGamboa CatarinaGamboa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@rcosta358 rcosta358 merged commit b98a110 into main May 11, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants